package advance.app.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author : fafa
 * @version V1.0
 * @Project: demo
 * @Package advance.app
 * @Description: TODO
 * @date Date : 2019年11月19日 11:47
 */
public class utils {
    /**

      * 下载zip文件
      * 返回zip的文件路径
      */
    public static String downLoadZip(String downloadURL){
        int bytesum = 0;
        int byteread = 0;
        Date date=new Date();

        SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd");
        String dateFloder= sf1.format(date);

        InputStream inStream=null;
        FileOutputStream fs =null;
        try {
        //URL url = new URL(downloadURL + "/" + dateFloder + "/" + "page.zip");
            URL url = new URL("http://localhost:8080/testzip/zip/1.zip");
            URLConnection conn = url.openConnection();
            inStream = conn.getInputStream();
            fs = new FileOutputStream(downloadURL);
            byte[] buffer = new byte[4028];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
            System.out.println("文件下载成功.....");
        } catch (Exception e) {
            System.out.println("下载异常"+e);
            return "false";
        } finally{
            try {
                if(inStream!=null){
                    inStream.close();
                }
            } catch (IOException e) {
                inStream=null;
            }
            try {
                if(fs!=null){
                    fs.close();
                }
            } catch (IOException e) {
                fs=null;
            }
        }
        return downloadURL;
    }

}
